package controller;

import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;

import model.Teacher;
import service.DeviceService;
import service.PresenceService;
import util.DeviceNotifier;

public class PresenceManager {

    @EJB
    PresenceService presenceService;
    @EJB
    DeviceService deviceService;
    private ArrayList<Teacher> present;
    private ArrayList<Teacher> absent;
    private Teacher selected;

    @PostConstruct
    public void init() {

        present = presenceService.loadPresence(true);
        absent = presenceService.loadPresence(false);
    }

    public ArrayList<Teacher> getPresent() {

        return this.present;
    }

    public boolean getHasPresent() {

        return this.present.size() > 0;
    }

    public ArrayList<Teacher> getAbsent() {

        return this.absent;
    }

    public boolean getHasAbsent() {

        return this.absent.size() > 0;
    }

    public void setSelected(Teacher selected) {

        this.selected = selected;
    }

    public String callAbsent() {

        presenceService.setPresence(this.selected.getId(), false);
        absent.add(this.selected);
        present.remove(this.selected);

        ArrayList<String> devices = deviceService.getDevicesByTeacher(this.selected.getCode());
        DeviceNotifier.notify(devices, this.selected, true);

        return "present";
    }

    public String callPresent() {

        presenceService.setPresence(this.selected.getId(), true);
        present.add(this.selected);
        absent.remove(this.selected);

        ArrayList<String> devices = deviceService.getDevicesByTeacher(this.selected.getCode());
        DeviceNotifier.notify(devices, this.selected, false);

        return "present";
    }
}